//////////////////////////////////////////////////////////////////////////////
//
//  Created by Philip Mulcahy on 08/01/2012.
//  Copyright (c) 2012 Philip Mulcahy. All rights reserved.
//
//  This file is part of the note-recog library.
//
//  note-recog is free software: you can redistribute it and/or modify
//  it under the terms of version 3 of the GNU Lesser General Public License 
//  as published by the Free Software Foundation.
//
//  note-recog is distributed is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with note-recog.  If not, see <http://www.gnu.org/licenses/>.
//
//////////////////////////////////////////////////////////////////////////////

#import "SliceTest.h"
#import <vector>
#import "Slice.h"
#import "Harmonic.h"
#import <cassert>
#import "Spectrum.h"

using namespace std;

@implementation SliceTest

- (void) test1
{
    vector<float> fft;
    fft.push_back(1);  //0
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(8);
    fft.push_back(10); //5
    fft.push_back(7);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0);  //10
    fft.push_back(1);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(1);  //15
    
    const std::vector<short> empty;
    
    //32.0 hz sampling rate implied by 16 bin absolute spectrum
    const Spectrum spec( fft.begin(), fft.end(), 32.f );
    
    const Slice s(0,0.0,spec, empty);
    
    const Harmonic& hf = s.fundamental();
    const Harmonic& hd = s.dominant();
    
    STAssertEquals( 5ul, hd.bin(), @"Dominant bin" );
    STAssertEquals( 5ul, hf.bin(), @"Fundamental bin" );
    STAssertEquals( &hd, &hf, @"identity" );

    STAssertEqualsWithAccuracy( 4.5f, hf.freq(), 5.f, @"frequency" );
    
    STAssertEqualsWithAccuracy( 2.25f, hf.halfPowerBw(), 0.25f, 
                                @"half power bandwidth" );
      
    STAssertEquals( 16ul, s.binCount(), @"binCount" );
    STAssertEquals( 1.f, s.binWidthHz(), @"binWidthHz" );
}

- (void) test2
{
    vector<float> fft;
    fft.push_back(0); //0
    fft.push_back(1.999);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0); //5
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0); //10
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(0);
    fft.push_back(2);
    fft.push_back(0); //15
    
    const std::vector<short> empty;
    
    const Spectrum spec( fft.begin(), fft.end(), fft.size() * 2. );
    
    STAssertEqualsWithAccuracy( spec.binwidthhz(), 1.f, 0.001f, @"binwidthhz" );
    
    const Slice s(0,0.0,spec, empty);
    
    const Harmonic& hd = s.dominant();
    STAssertEqualsWithAccuracy( 14.f, hd.freq(), 0.1f, @"frequency" );
    STAssertEquals( 14ul, hd.bin(), @"dominant bin" );

    const Harmonic& hf = s.fundamental();
    STAssertEqualsWithAccuracy( 1.f, hf.freq(), 0.1f, @"frequency" );
}

@end
